<!--  -->
<template>
  <div id="ProductList">
    <div class="hint">
      <hint></hint>
    </div>
    <div class="ProductList-top">
      <div class="form-top">商品筛选</div>
      <el-form class="form" :model="siftform" ref="from">
        <el-form-item label="所属店铺:" class="form-left">
          <el-select
            v-model="siftform.name"
            placeholder="请选择店铺"
            class="form-select"
          >
            <el-option
              v-for="item in dp"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="商品分类:" class="form-mid">
          <el-select
            v-model="siftform.type"
            placeholder="请选择商品类型"
            class="form-select"
          >
            <el-option
              v-for="item in type"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item class="form-right1">
          <el-select
            v-model="siftform.partition"
            placeholder="请选择商品类型"
            class="form-select"
          >
            <el-option
              v-for="item in partition"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item> -->
        <el-form-item class="form-right">
          <el-button type="primary" class="submitbutton" @click="getproduct"
            >查询</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <div class="ProductList-content">
      <div class="table-top">
        <div>所有数据</div>
        <div class="add" @click="addproduct">添加商品</div>
      </div>
      <el-table
        class="table"
        ref="multipleTable"
        :data="tableData.slice((currpage - 1) * pagesize, currpage * pagesize)"
        tooltip-effect="dark"
        style="width: 100%"
      >
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column prop="name" label="所属店铺" width="120">
        </el-table-column>
        <el-table-column prop="partition" label="商品名称" width="120">
        </el-table-column>
        <el-table-column prop="type" label="商品分类"> </el-table-column>
        <el-table-column prop="num" label="数量"> </el-table-column>
        <el-table-column prop="canteenPrice" label="堂食价格">
        </el-table-column>
        <el-table-column prop="vipPrice" label="会员价"> </el-table-column>
        <el-table-column prop="costPrice" label="成本价"> </el-table-column>
        <el-table-column prop="sellingPrice" label="外卖价"> </el-table-column>
        <el-table-column prop="delivery" label="是否上架"> </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-pagination
      @size-page="handlesizepage"
      @current-change="handlecurrentchange"
      :page-size="pagesize"
      background
      layout="prev, pager, next"
      :total="1000"
    >
    </el-pagination>
  </div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';
import hint from "./header";

export default {
  //import引入的组件需要注入到对象中才能使用
  components: {
    hint: hint,
  },
  data() {
    //这里存放数据
    return {
      pagesize: 10, //每页显示条数
      currpage: 1, //初始页
      // rules: {
      //   name: {
      //     required: true,
      //     message: "请选择店铺",
      //   },
      //   partition: {
      //     required: true,
      //     message: "请选择商品",
      //   },
      //   type: {
      //     required: true,
      //     message: "请选择商品类型",
      //   },
      // },
      value: "",
      siftform: {
        name: "",
        partition: "",
        type: "",
      },
      dp: [
        {
          value: "肯德基",
          label: "肯德基",
        },
        {
          value: "华莱士",
          label: "华莱士",
        },
        {
          value: "麦当劳",
          label: "麦当劳",
        },
        {
          value: "汉堡王",
          label: "汉堡王",
        },
        {
          value: "必胜客",
          label: "必胜客",
        },
        {
          value: "德克士",
          label: "德克士",
        },
      ],
      type: [
        {
          value: "热菜",
          label: "热菜",
        },
        {
          value: "凉菜",
          label: "凉菜",
        },
      ],
      partition: [
        {
          value: "麦旋风",
          label: "麦旋风",
        },
        {
          value: "奥尔良鸡翅",
          label: "奥尔良鸡翅",
        },
        {
          value: "鸡块",
          label: "鸡块",
        },
      ],

      tableData: [
        // {
        //   name: "肯德基",
        //   partition: "麦旋风",
        //   type: "热菜",
        //   num: 1,
        //   canteenPrice: 20,
        //   vipPrice: 17,
        //   costPrice: 10,
        //   sellingPrice: 25,
        //   delivery: "上架",
        // },
      ],
    };
  },
  //监听属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {},
  //方法集合
  methods: {
    getproduct: function () {
      console.log(this.siftform);
      let name = this.siftform.name;
      let partition = this.siftform.partition;
      let type = this.siftform.type;
      console.log(name, partition, type);
      let tableData = this.tableData;
      
      // let newtableData = [];
      if (name) {
        this.tableData = tableData.filter(function (obj) {
          return obj.name == name;
        });
      }
    //   if (partition!=a) {
    //     this.tableData = tableData.filter(function (obj) {
    //       return obj.name == partition;
    //     });
    //  console.log("1");
    //   }
    //   if (type!=a) {
    //     this.tableData = tableData.filter(function (obj) {
    //       return obj.name == type;
    //     });
    //   }

      // console.log(tabafilter);
      // this.tableData=tabafilter

      //  newtableData=tabafilter
      // tableData.forEach(function (item, index) {
      //   if (
      //     (item.name == name) &&
      //     (item.partition == partition) &&
      //     (item.type == type)
      //   ) {
      //     console.log(index);
      //     console.log(tableData[index]);
      //     newtableData.push(tableData[index]);
      //     this.tableData = newtableData;

      //   } else {
      //     // alert("没有找到该类型商品");
      //     // this.tableData = tableData;

      //   }
      // });
    },
    handlesizepage: function (val) {
      this.pagesize = val;
    },
    handlecurrentchange: function (val) {
      this.currpage = val;
    },
    addproduct: function () {
      // console.log(this.$route.name);
      this.$router.push("productdetails");
    },

    handleDelete(index) {
      this.tableData.splice(index, 1);
    },
    chandpage: function () {},
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {
    this.$http
      .get("http://rap2api.taobao.org/app/mock/276606/getshoplist", {})
      .then((res) => {
        // console.log(res.data.data.productlist);
        this.tableData = res.data.data.productlist;
        if (this.$store.state.newproduct.name === undefined) {
          return;
        } else {
          if (this.$store.state.newproduct.delivery == true) {
            this.$store.state.newproduct.delivery = "上架";
          } else {
            this.$store.state.newproduct.delivery = "下架";
          }
          this.tableData.unshift(this.$store.state.newproduct);
        }
      });
  },
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {
    // console.log(this.tableData);
    //  this.tableData.push(this.$store.state.newproduct)
  },
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {}, //如果页面有keep-alive缓存功能，这个函数会触发
};
</script>

<style lang="less" scoped>
.ProductList-top {
  border: solid 1px #e9e9e9;

  .form-top {
    border-bottom: solid 1px #e9e9e9;
    height: 50px;
    line-height: 50px;
  }

  .form {
    display: flex;
    height: 60px;
    padding-top: 30px;
    padding-left: 50px;
    .form-left {
      display: flex;
      width: 400px;
      .form-select {
        width: 300px;
      }
    }
    .form-right1 {
      width: 200px;
      margin-right: 10px;
      .form-input {
      }
    }
    .form-mid {
      display: flex;
      width: 400px;
      .form-select {
        width: 300px;
      }
    }

    .form-right {
      display: flex;
      .submitbutton {
        width: 300px;
      }
    }
  }
}
.ProductList-content {
  .table {
    min-height: 600px;
  }
  .table-top {
    display: flex;
    padding-left: 10px;
    padding-right: 50px;
    height: 50px;
    line-height: 50px;
    justify-content: space-between;
    span {
    }
    .add {
      width: 150px;
      height: 50px;
      background-color: #2f88ff;
      color: #fff;
      text-align: center;
      line-height: 50px;
      border-radius: 5px;
    }
  }
}
</style>